﻿@using OBUCare.Web.Models
@using OBUCare.Core.Common
@using OBUCare.Web.Infrastructure
@model IEnumerable<ImportExcelDataViewModel>
@{
    ViewBag.Title = "Index";
}
<link href="../../Content/tableCss.css" rel="stylesheet" type="text/css" />
<h2>厂商反馈信息</h2>

<table width="900px">
    <tr>

        <td>
            <div>
                @(Html.Telerik().Upload()
                            .Name("upload_attachments")
                            .Multiple(false).ShowFileList(true)
                            .Async
                                (async => async
                                    .Save("Save", "Upload")
                                    .Remove("Remove", "Upload")
                                    .AutoUpload(true)
                                )
                            .ClientEvents(
                                c => c.OnSuccess("onUploadSuccess")
                                )
                            )


            </div>
        </td>
        <td>
            <input type="button" id="btnImport" value="导入" />
        </td>
    </tr>

</table>
<div id="divPreviewGrid">

</div>


<script>

    $().ready(function () {
        $("#btnImport").attr("disabled", true);

        $("#btnImport").click(function ()
        {
            Import(fileName);
        });
    });

    //动态加载Excel局部视图
    function loadExcelPreviewControl(fileName) {
        //验证上传的excel数据是否正确
        if (checkImportExcelData(fileName)) {

            var url = '/Import/ExcelPreviewGrid/' + fileName;
            $('#divPreviewGrid').load(url, function (response, status, xhr) {

                if ("error" == status) {
                    alert("加载Excel数据失败: " + response);
                }
                else {

                    //$('#divPreviewGrid').show();  
                    $("#btnImport").attr("disabled", false);
                }
            });
        }
    }

    function checkImportExcelData(fileName)
    {
        var result = false; //是否通过验证
        
        $.ajax({
            async: false, //使用同步的Ajax请求  
            type: "POST",
            url: "/Import/CheckExcelPreviewGrid/" + fileName,
            data: null,
            success: function (data) {
                if (data.success) {
                    result = data.success;
                }
                else {
                    alert(data.message)
                }
            }
        });

        return result;
    }

    //保存从服务器回传的已经上传功能的文件名
    var fileName = null;
    function onUploadSuccess(e) {

        if (e.response.status == "OK") {
            if (e.operation == "upload") {
                for (i = 0; i < e.response.Files.length; i++) {
                    var f = e.response.Files[i];
                    fileName = f;
                    loadExcelPreviewControl(fileName);
                    //$("#btnImport").attr("disabled", false);
                }
            }
            else {
                fileName = null;
            }
        }

    }

    function Import(fileName) {
                 
        if (checkBeforeImport(fileName)) {

            var jdata = { id: fileName };


            $.post("/Import/ConfirmImport", jdata,
                        function (data) {
                            // Successful requests get here
                            // Update the page elements

                            if (data.success) {
                                alert("导入成功");
                                location.href = "/RepairOrders/List/"; //导入成功后，跳转至维修单管理界面
                            }
                            else {
                                alert("导入失败:" + data.message)
                            }
                        });
        }
    }

    function checkBeforeImport(fileName) {
        var result = false; //是否通过验证

        $.ajax({
            async: false, //使用同步的Ajax请求  
            type: "POST",
            url: "/Import/CheckBeforImport/" + fileName,
            data: null,
            success: function (data) {
                if (data.success) {
                    result = data.success;
                }
                else {
                    //是否已经导入过数据
                    if (data.hasImportBefore)
                    {
                        if (confirm(data.message))
                            result = true;
                        else
                            result = false;
                    }
                    else
                        alert(data.message)
                }
            }
        });

        return result;
    }
</script>
